package com.ehualu.msg.util;

import java.io.IOException;
import java.io.StringReader;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;

import com.ehualu.core.util.sms.pojo.WhjgSmsResult;


/**
 * 工具类
 * @author Administrator
 *
 */
public class XMLUtil {

	private static final Logger logger = LoggerFactory.getLogger(XMLUtil.class);
	/**
	 * 将中文字符转化为UTF-8格式
	 * @param xmlDoc
	 * @return
	 */
	public static String encodeUTF8(String xmlDoc){
		String str = "";
		try {
			str = URLEncoder.encode(xmlDoc, "UTF-8");
		} catch (Exception e) {
			str = e.toString();
			
		}
		return str;
	}

	/**
	 * 将UTF-8格式字符转换为中文字符
	 * @param str
	 * @return
	 */
	public static String decodeUTF8(String str){
		String xmlDoc = "";
		try {
			xmlDoc = URLDecoder.decode(str, "UTF-8");
		} catch (Exception e) {
			xmlDoc = e.toString();
		}
		return xmlDoc;
	}
	
	/**
	 * 武汉短信webservice接口用
	 * 根据传入参数生成查询XML格式串
	 * 节点标签为QueryCondition
	 * @param map
	 * @return
	 */
	public static String createXmlParam(Map<String,Object> map){
		StringBuffer queryXml = new StringBuffer();
		queryXml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		queryXml.append("<sms>");
		for(Map.Entry<String, Object> entry:map.entrySet()){
			queryXml.append("<")
			.append(entry.getKey())
			.append(">")
			.append(entry.getValue() == null ? "" : entry.getValue().toString())
			.append("</")
			.append(entry.getKey())
			.append(">");
		}
		queryXml.append("</sms>");
		return queryXml.toString();
	}
	/**
	 * 辽宁webservice接口用
	 * @param xmlDoc
	 * @return
	 */
	public static WhjgSmsResult parseXml(String xmlDoc){
        StringReader read = new StringReader(xmlDoc);
        InputSource source = new InputSource(read);
        SAXBuilder sb = new SAXBuilder();
        Document doc = null;
        try {
            doc = sb.build(source);
        } catch (Exception e) {
        	logger.error(e.getMessage());
        }
        Element root = doc.getRootElement();
        //head解析
        Element head = root.getChild("head");
        Element code = head.getChild("code");
        Element message = head.getChild("message");
        logger.info("----------------短信发送响应结果------------------"+code.getValue()+"="+message.getValue());
        WhjgSmsResult ws = new WhjgSmsResult();
        ws.setCode(code.getValue());
        ws.setMessage(message.getValue());
        return ws;
	}
	
	public static void main(String[] args) throws IOException {
        String xmlDoc = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><head><code>1</code><message>提示信息</message></head></root>";
        XMLUtil.parseXml(xmlDoc);
		
		
		Map<String,Object> map1  = new HashMap<String, Object>();
		map1.put("user", "admin");
		map1.put("sendType", "1");
		map1.put("sendTime", "");
		map1.put("phone", "18701309209");
		map1.put("smscontext", "短信接口测试");
		map1.put("operator", "");
		String xmlDoccq = XMLUtil.createXmlParam(map1);
		System.out.println(xmlDoccq);
		
		
	}
}
